<template>
  <div>
    <!-- 增加查询重置组件 -->
    <!-- inline="true" 表单一行显示 -->
    <el-form
      ref="searchForm"
      :inline="true"
      :model="searchMap"
      class="demo-form-inline"
      style="margin-left:60px"
    >
      <el-form-item prop="accountNum">
        <el-input v-model="searchMap.accountNum" placeholder="账号"></el-input>
      </el-form-item>

      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="姓名"></el-input>
      </el-form-item>

      <el-form-item style="margin-left:40px">
        <el-button type="primary" @click="fetchData" class="btn">查询</el-button>
        <el-button type="primary" @click="add" class="btn">新增</el-button>
        <el-button @click="reset('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格组件 -->
    <!-- 注意在el组件表格标签中 可以直接添加样式属性 -->
    <el-table :data="list" style="width: 100%" border height="385px">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="accountNum" label="账号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="phoneNum" label="电话"></el-table-column>
      <el-table-column prop="salary" label="薪酬"></el-table-column>
      <el-table-column prop="entryTime" label="入职时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10,20,30,40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 编辑员工对话框组件 -->
    <el-dialog title="编辑员工信息" :visible.sync="isFormVisible" width="400px">
      <el-form
        ref="editForm"
        :model="pojo"
        style="width:300px"
        label-width="100px"
        label-position="right"
        :rules="rules"
        width="400px"
      >
        <el-form-item label="账号" prop="accountNum">
          <el-input v-model="pojo.accountNum"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="pojo.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="pojo.age"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phoneNum">
          <el-input v-model="pojo.phoneNum"></el-input>
        </el-form-item>
        <el-form-item label="薪酬" prop="salary">
          <el-input v-model="pojo.salary"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="entryTime">
          <el-input v-model="pojo.entryTime"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          :plain="true"
          @click="pojo.id!==null?updateStaffData('editForm'):submit('editForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//导入api

import staffApi from "@/api/staff";

export default {
  data() {
    return {
      searchMap: {
        accountNum: "",
        name: ""
      },
      list: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      isFormVisible: false,
      pojo: {
        accountNum: "",
        name: "",
        age: null,
        phoneNum: null,
        salary: null,
        entryTime: ""
      },
      rules: {
        accountNum: [
          { required: true, message: "账号不能为空", trriger: "blur" }
        ],
        name: [{ required: true, message: "姓名不能为空", trriger: "blur" }],
        entryTime: [
          { required: true, message: "入职时间不能为空", trriger: "blur" }
        ]
      }
    };
  },
  created() {
    this.fetchData();
  },

  methods: {
    fetchData() {
      staffApi
        .getList(this.currentPage, this.pageSize, this.searchMap)
        .then(response => {
          const resData = response.data;
          this.list = resData.data.rows;
          this.total = resData.data.total;
        })
        .catch(error => {});
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    //新增员工
    add() {
      this.isFormVisible = true;
      //重置表单
      this.$nextTick(() => {
        this.$refs["editForm"].resetFields();
      });
    },
    //重置表单
    reset(formName) {
      this.$refs[formName].resetFields();
    },
    //提交新增员工数据
    submit(formName) {
      this.$refs[formName].validate(valid => {
        //校验成功
        if (valid) {
          staffApi.add(this.pojo).then(response => {
            const resData = response.data;
            if (resData.flag) {
              // 1.弹出提示信息
              this.$message({
                message: resData.message,
                type: "success"
              });
              // 2,重新抓取最新的数据
              this.fetchData();
              //3.退出对话编辑框
              this.isFormVisible = false;
            } else {
              this.$message({
                message: resData.message,
                type: "error"
              });
              this.fetchData();
              this.isFormVisible = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    updateStaffData() {
      staffApi.update(this.pojo.id, this.pojo).then(response => {
        const resData = response.data;
        //如果更新成功
        if (resData.flag) {
          this.$message({
            message: resData.message,
            type: "success"
          });

          this.fetchData();
          this.isFormVisible = false;
        } else {
          this.$message({
            message: resData.message,
            type: "error"
          });
          this.fetchData();
          this.isFormVisible = false;
        }
      });
      this.pojo.id = null;
    },

    //根据id获取用户信息
    handleEdit(id) {
      this.isFormVisible = true;
      // console.log(id);
      staffApi.getStaffMsg(id).then(response => {
        const resData = response.data;
        if (resData.flag) {
          this.pojo = resData.data;
        } else {
          this.$message({
            message: resData.message,
            type: "error"
          });
        }
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该员工信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          staffApi.delete(id).then(response => {
            const resData = response.data;
            if (resData.flag) {
              this.$message({
                type: "success",
                message:resData.message
              });

              this.fetchData();
            }else{
              this.$message({
                message:resData.message,
                type:'error'
              });
              this.fetchData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>